package com.spring.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.blog.common.constant.Constants;
import com.spring.blog.common.util.BeanTransUtils;
import com.spring.blog.common.util.JWTUtils;
import com.spring.blog.common.util.SecurityUtil;
import com.spring.blog.exception.BlogException;
import com.spring.blog.mapper.BlogInfoMapper;
import com.spring.blog.mapper.UserInfoMapper;
import com.spring.blog.pojo.dataobject.BlogInfo;
import com.spring.blog.pojo.dataobject.UserInfo;
import com.spring.blog.pojo.request.UserLoginRequest;
import com.spring.blog.pojo.response.GetUserInfoResponse;
import com.spring.blog.pojo.response.UserLoginResponse;
import com.spring.blog.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getUserName,userLoginRequest.getUserName())
                .eq(UserInfo::getDeleteFlag, Constants.NORMAL);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        //进行校验
        if(userInfo==null||userInfo.getId()<1||userInfo.getId()==null){
           throw new BlogException("用户不存在");
        }
        if(!SecurityUtil.verify(userLoginRequest.getPassword(),userInfo.getPassword())){
            throw new BlogException("用户密码不正确");
        }
            Map<String,Object> map=new HashMap<>() ;
            //将Id和UserName写入载荷
            map.put("id",userInfo.getId());
            map.put("name",userInfo.getUserName());
            String s = JWTUtils.genJwt(map);
            return new UserLoginResponse(userInfo.getId(),s);
        }

    @Override
    public GetUserInfoResponse getAuthorInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getId,blogId);
        BlogInfo blogInfo = blogInfoMapper.selectOne(queryWrapper);
        if(blogInfo==null||blogInfo.getId()<1){
            throw new BlogException("错误");
        }
        QueryWrapper<UserInfo> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.lambda().eq(UserInfo::getId,blogInfo.getUserId())
                .eq(UserInfo::getDeleteFlag,Constants.NORMAL);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper1);
        return BeanTransUtils.transgetuserinforesponse(userInfo);
    }
    @Override
    public GetUserInfoResponse getUserInfo(Integer userId) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getDeleteFlag,Constants.NORMAL)
                .eq(UserInfo::getId,userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        return BeanTransUtils.transgetuserinforesponse(userInfo);
    }
}
